package com.yuan.untils;

import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：YuanHaHa
 * @Date：2024/7/4 16:53
 */
public class ListUtils {

    public static <T> List<List<T>> splitsListPage(List<T> resList, int pageSize){
        if (CollectionUtils.isEmpty(resList) || pageSize <= 0){
            return Lists.newArrayList();
        }
        int length = resList.size();
        int num = (length + pageSize - 1) / pageSize;
        List<List<T>> newList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            int fromIndex = i * pageSize;
            int toIndex = (i + 1) * pageSize < length ? (i + 1) * pageSize : length;
            newList.add(resList.subList(fromIndex, toIndex));
        }
        return newList;
    }
}
